package my.multithreadtest;
class SynchronizedEvenGenerator implements Invariant{
	private int i;
	public synchronized void next(){
		i++;
		i++;
	}
	public synchronized int getValue(){return i;}
	@Override
	public InvariantState invariant() {
		int val=getValue();
		if(val%2==0){
//			System.out.println("syn " +val);
			return new InvariantOK();
		}
		else return new InvariantFailure(new Integer(val));
	}

}
public class EvenGenerator implements Invariant{
	private int i;
	public void next(){
		i++;
		i++;
	}
	public int getValue(){return i;}
		
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		EvenGenerator gen= new EvenGenerator();
		SynchronizedEvenGenerator syngen=new SynchronizedEvenGenerator();
		new InvariantWatcher(gen);
//		new InvariantWatcher(syngen);
		while(true){
			gen.next();
			//syngen.next();
		}
		
		
	}

	@Override
	public InvariantState invariant() {
		int val=getValue();
		if(val%2==0){
			System.out.println("x "+val);
			return new InvariantOK();
		}
		else return new InvariantFailure(new Integer(val));
	}

}
